{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "026d2ca7-8a14-4cc5-81f0-ec319f63e442",
   "metadata": {},
   "outputs": [],
   "source": [
    "from SammBaseLib.LogicSamm import *\n",
    "from SammBaseLib.WidgetSammBase import *\n",
    "import SampleData\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "211fc502-4c29-4b4a-a4be-c173b775021b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# nodeToClone = SampleData.SampleDataLogic().downloadMRHead()\n",
    "# nodeToClone = SampleData.SampleDataLogic().downloadCTChest()\n",
    "# nodeToClone = SampleData.SampleDataLogic().downloadCTACardio()\n",
    "\n",
    "slicer.util.selectModule(\"SammBase\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "642430aa-9f67-4af0-940f-540047fe92a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "143.6402877697842\n",
      "[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   0   3   1   0   0   0   0   0   0   0   0   0   0   0   0   3\n",
      "   0   0   0   0   0   0   0   0   0   0   0   0   0   3   0   0   0   0\n",
      "   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   8   0   0   1  32  55  56  46  42  49  56  56  53  60  74  76\n",
      "  69  76  90  94  94  96 105 106 101 106  99  78  81 110 121 108 103 112\n",
      " 108 103 112 119 112 112 119 121 124 128 146 151 146 160 181 187 197 219\n",
      " 231 226 217 224 231 217 212 226 226 215 215 201 172 158 156 146 126 119\n",
      " 117 119 130 128 101  76  55  26   5   0   3  12   3   3  12   5   0   1\n",
      "   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0\n",
      "   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n",
      "   0   0   0   0]\n"
     ]
    }
   ],
   "source": [
    "import vtk\n",
    "\n",
    "logic = SammBaseLogic()\n",
    "# logic._parameterNode.SetNodeReferenceID(\"sammPrompt2DBox\", \"vtkMRMLMarkupsPlaneNode1\")\n",
    "# plane = logic._parameterNode.GetNodeReference(\"sammPrompt2DBox\")\n",
    "# points = vtk.vtkPoints() \n",
    "# plane.GetPlaneCornerPoints(points)\n",
    "# print(points)\n",
    "# print(points.GetPoint(0)[2])\n",
    "\n",
    "logic._parameterNode.SetNodeReferenceID(\"sammInputVolume\", \"vtkMRMLScalarVolumeNode1\")\n",
    "volumeNodeDataPointer = slicer.util.arrayFromVolume(\n",
    "    logic._parameterNode.GetNodeReference(\"sammInputVolume\"))\n",
    "volumeDisplayNode = logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetDisplayNode()\n",
    "window = volumeDisplayNode.GetWindow()\n",
    "level = volumeDisplayNode.GetLevel()\n",
    "print(window)\n",
    "imageMin = level - window / 2\n",
    "imageMax = level + window / 2\n",
    "imageNormalized = (((copy.deepcopy(volumeNodeDataPointer) - imageMin).astype(\"float32\") / (imageMax - imageMin)) * 256)\n",
    "imageNormalized[imageNormalized<0] = 0\n",
    "imageNormalized[imageNormalized>255] = 255\n",
    "imageNormalized = imageNormalized.astype(numpy.uint8)\n",
    "print(imageNormalized[127][127])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c6c9240e-10dd-4f8b-8faa-aa23d9aebf61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<MRMLCore.vtkMRMLScalarVolumeNode(0xd1327f0) at 0x7f36ab2ba940>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "logic = SammBaseLogic()\n",
    "logic._parameterNode.SetNodeReferenceID(\"sammInputVolume\", \"vtkMRMLScalarVolumeNode1\")\n",
    "# imageNormalized = logic.processSlicePreProcess()\n",
    "# print(imageNormalized.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "500c57e2-7492-42f8-bae8-e3f143b05158",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vtkMRMLScalarVolumeNode (0xcf59b90)\n",
      "  ID: vtkMRMLScalarVolumeNode1\n",
      "  ClassName: vtkMRMLScalarVolumeNode\n",
      "  Name: DZ-CBCT_1\n",
      "  Debug: false\n",
      "  MTime: 1094405\n",
      "  Description: (none)\n",
      "  SingletonTag: (none)\n",
      "  HideFromEditors: false\n",
      "  Selectable: true\n",
      "  Selected: false\n",
      "  UndoEnabled: false\n",
      "  Node references:\n",
      "    display [displayNodeRef]: vtkMRMLScalarVolumeDisplayNode1\n",
      "    storage [storageNodeRef]: (none)\n",
      "    transform [transformNodeRef]: (none)\n",
      "  TransformNodeID: (none)\n",
      "  DisplayNodeIDs[0]: vtkMRMLScalarVolumeDisplayNode1\n",
      "  Spacing: (0.25, 0.25, 0.25)\n",
      "  Origin: (90.1896, 136.381, -49.9637)\n",
      "  VoxelVectorType: undefined\n",
      "  IJKToRASDirections:\n",
      "    -0.994078 -0.0969441 0.0490957\n",
      "    0.0913317 -0.990171 -0.105923\n",
      "    0.0588818 -0.100812 0.993162\n",
      "  ImageData:\n",
      "    Debug: Off\n",
      "    Modified Time: 1058346\n",
      "    Reference Count: 4\n",
      "    Registered Events: \n",
      "      Registered Observers:\n",
      "        vtkObserver (0xc48f1f0)\n",
      "          Event: 33\n",
      "          EventName: ModifiedEvent\n",
      "          Command: 0xbae2ec0\n",
      "          Priority: 0\n",
      "          Tag: 1\n",
      "    Information: 0xd852800\n",
      "    Data Released: False\n",
      "    Global Release Data: Off\n",
      "    UpdateTime: 1983058\n",
      "    Field Data:\n",
      "      Debug: Off\n",
      "      Modified Time: 1058300\n",
      "      Reference Count: 1\n",
      "      Registered Events: (none)\n",
      "      Number Of Arrays: 0\n",
      "      Number Of Components: 0\n",
      "      Number Of Tuples: 0\n",
      "    Number Of Points: 192636937\n",
      "    Number Of Cells: 191616192\n",
      "    Cell Data:\n",
      "      Debug: Off\n",
      "      Modified Time: 1058342\n",
      "      Reference Count: 1\n",
      "      Registered Events: \n",
      "        Registered Observers:\n",
      "          vtkObserver (0x863b400)\n",
      "            Event: 33\n",
      "            EventName: ModifiedEvent\n",
      "            Command: 0xd8529c0\n",
      "            Priority: 0\n",
      "            Tag: 1\n",
      "      Number Of Arrays: 0\n",
      "      Number Of Components: 0\n",
      "      Number Of Tuples: 0\n",
      "      Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 1 1 1 )\n",
      "      Interpolate Flags: ( 1 1 1 1 1 0 0 1 1 1 1 )\n",
      "      Pass Through Flags: ( 1 1 1 1 1 1 1 1 1 1 1 )\n",
      "      Scalars: (none)\n",
      "      Vectors: (none)\n",
      "      Normals: (none)\n",
      "      TCoords: (none)\n",
      "      Tensors: (none)\n",
      "      GlobalIds: (none)\n",
      "      PedigreeIds: (none)\n",
      "      EdgeFlag: (none)\n",
      "      Tangents: (none)\n",
      "      RationalWeights: (none)\n",
      "      HigherOrderDegrees: (none)\n",
      "    Point Data:\n",
      "      Debug: Off\n",
      "      Modified Time: 1058346\n",
      "      Reference Count: 1\n",
      "      Registered Events: \n",
      "        Registered Observers:\n",
      "          vtkObserver (0xd852ba0)\n",
      "            Event: 33\n",
      "            EventName: ModifiedEvent\n",
      "            Command: 0xd8529c0\n",
      "            Priority: 0\n",
      "            Tag: 1\n",
      "      Number Of Arrays: 1\n",
      "      Array 0 name = ImageScalars\n",
      "      Number Of Components: 1\n",
      "      Number Of Tuples: 192636937\n",
      "      Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 1 1 1 )\n",
      "      Interpolate Flags: ( 1 1 1 1 1 0 0 1 1 1 1 )\n",
      "      Pass Through Flags: ( 1 1 1 1 1 1 1 1 1 1 1 )\n",
      "      Scalars: \n",
      "        Debug: Off\n",
      "        Modified Time: 1058058\n",
      "        Reference Count: 1\n",
      "        Registered Events: (none)\n",
      "        Name: ImageScalars\n",
      "        Data type: short\n",
      "        Size: 192636937\n",
      "        MaxId: 192636936\n",
      "        NumberOfComponents: 1\n",
      "        Information: 0x862a700\n",
      "          Debug: Off\n",
      "          Modified Time: 1058709\n",
      "          Reference Count: 1\n",
      "          Registered Events: (none)\n",
      "          PER_COMPONENT: vtkInformationVector(0x862aab0)\n",
      "        Name: ImageScalars\n",
      "        Number Of Components: 1\n",
      "        Number Of Tuples: 192636937\n",
      "        Size: 192636937\n",
      "        MaxId: 192636936\n",
      "        LookupTable: (none)\n",
      "      Vectors: (none)\n",
      "      Normals: (none)\n",
      "      TCoords: (none)\n",
      "      Tensors: (none)\n",
      "      GlobalIds: (none)\n",
      "      PedigreeIds: (none)\n",
      "      EdgeFlag: (none)\n",
      "      Tangents: (none)\n",
      "      RationalWeights: (none)\n",
      "      HigherOrderDegrees: (none)\n",
      "    Bounds: \n",
      "      Xmin,Xmax: (0, 666)\n",
      "      Ymin,Ymax: (0, 666)\n",
      "      Zmin,Zmax: (0, 432)\n",
      "    Compute Time: 1157292\n",
      "    Spacing: (1, 1, 1)\n",
      "    Origin: (0, 0, 0)\n",
      "    Direction: (1, 0, 0, 0, 1, 0, 0, 0, 1)\n",
      "    Dimensions: (667, 667, 433)\n",
      "    Increments: (0, 0, 0)\n",
      "    Extent: (0, 666, 0, 666, 0, 432)\n",
      "\n",
      "\n",
      "[[-0.9940782  -0.09694405  0.04909572]\n",
      " [ 0.09133168 -0.99017111 -0.10592306]\n",
      " [ 0.05888177 -0.10081181  0.99316158]]\n"
     ]
    }
   ],
   "source": [
    "# s = logic._parameterNode.GetNodeReference(\"sammInputVolume\")\n",
    "\n",
    "# s.SetIJKToRASDirections(numpy.array([[1,0,0],[0,1,0],[0,0,1]]))\n",
    "IjkToRasDir = numpy.array([[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]])\n",
    "print(logic._parameterNode.GetNodeReference(\"sammInputVolume\"))\n",
    "logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetIJKToRASDirections(IjkToRasDir)\n",
    "print(IjkToRasDir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8829b5ee-0880-4082-b952-c7dd0165f9b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vtkMatrix4x4 (0xc4c0730)\n",
      "  Debug: Off\n",
      "  Modified Time: 2040760\n",
      "  Reference Count: 1\n",
      "  Registered Events: (none)\n",
      "  Elements:\n",
      "    1 0 0 0 \n",
      "    0 1 0 0 \n",
      "    0 0 1 0 \n",
      "    0 0 0 1 \n",
      "\n",
      "\n",
      "vtkMatrix4x4 (0xc4c0730)\n",
      "  Debug: Off\n",
      "  Modified Time: 2040771\n",
      "  Reference Count: 1\n",
      "  Registered Events: (none)\n",
      "  Elements:\n",
      "    0 -1 0 133.929 \n",
      "    -0 0 -1 116.786 \n",
      "    0.769233 0 0 66.6502 \n",
      "    -0 0 -0 1 \n",
      "\n",
      "\n",
      "vtkMatrix4x4 (0xc4c0730)\n",
      "  Debug: Off\n",
      "  Modified Time: 2040771\n",
      "  Reference Count: 1\n",
      "  Registered Events: (none)\n",
      "  Elements:\n",
      "    0 -1 0 133.929 \n",
      "    -0 0 -1 116.786 \n",
      "    0.769233 0 0 66.6502 \n",
      "    -0 0 -0 1 \n",
      "\n",
      "\n"
     ]
    },
    {
     "ename": "<class 'AttributeError'>",
     "evalue": "'vtkmodules.vtkCommonMath.vtkMatrix4x4' object has no attribute 'transpose'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[31], line 16\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28mprint\u001b[39m(IjkToRasDir)\n\u001b[1;32m     15\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m3\u001b[39m):\n\u001b[0;32m---> 16\u001b[0m     logic\u001b[38;5;241m.\u001b[39m_parameterNode\u001b[38;5;241m.\u001b[39mRGYNpArrOrder[i] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m\u001b[38;5;241m-\u001b[39mnumpy\u001b[38;5;241m.\u001b[39margmax(numpy\u001b[38;5;241m.\u001b[39mabs(IjkToRasDir\u001b[38;5;241m.\u001b[39mtranspose()[i]))\n\u001b[1;32m     17\u001b[0m metadata \u001b[38;5;241m=\u001b[39m [ \\\n\u001b[1;32m     18\u001b[0m     [], \\\n\u001b[1;32m     19\u001b[0m     logic\u001b[38;5;241m.\u001b[39m_parameterNode\u001b[38;5;241m.\u001b[39mRGYNpArrOrder\n\u001b[1;32m     20\u001b[0m ]\n\u001b[1;32m     21\u001b[0m logic\u001b[38;5;241m.\u001b[39m_parameterNode\u001b[38;5;241m.\u001b[39m_volMetaData \u001b[38;5;241m=\u001b[39m metadata\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'vtkmodules.vtkCommonMath.vtkMatrix4x4' object has no attribute 'transpose'"
     ]
    }
   ],
   "source": [
    "inModel         = logic._parameterNode.GetNodeReference(\"sammInputVolume\")\n",
    "imageData       = slicer.util.arrayFromVolume(inModel)\n",
    "imageDataShape  = imageData.shape\n",
    "logic._segNumpy  = numpy.zeros(imageDataShape)\n",
    "\n",
    "# get axis directions aligning RGY views (need to optimize here)\n",
    "IjkToRasDir  = vtk.vtkMatrix4x4()\n",
    "print(IjkToRasDir)\n",
    "logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetRASToIJKMatrix(IjkToRasDir)\n",
    "print(IjkToRasDir)\n",
    "\n",
    "\n",
    "logic._parameterNode.RGYNpArrOrder = [0, 0, 0]\n",
    "print(IjkToRasDir)\n",
    "for i in range(3):\n",
    "    logic._parameterNode.RGYNpArrOrder[i] = 2-numpy.argmax(numpy.abs(IjkToRasDir.transpose()[i]))\n",
    "metadata = [ \\\n",
    "    [], \\\n",
    "    logic._parameterNode.RGYNpArrOrder\n",
    "]\n",
    "logic._parameterNode._volMetaData = metadata\n",
    "\n",
    "volumeNodeDataPointer = slicer.util.arrayFromVolume(\n",
    "            logic._parameterNode.GetNodeReference(\"sammInputVolume\"))\n",
    "volumeDisplayNode = logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetDisplayNode()\n",
    "window = volumeDisplayNode.GetWindow()\n",
    "level = volumeDisplayNode.GetLevel()\n",
    "\n",
    "imageMin = level - window / 2\n",
    "imageMax = level + window / 2\n",
    "print(logic._parameterNode.RGYNpArrOrder)\n",
    "imageNormalized = (((copy.deepcopy(volumeNodeDataPointer) - imageMin).astype(\"float32\") / (imageMax - imageMin)) * 256).astype(numpy.uint8)\n",
    "imageNormalized = imageNormalized.transpose(2-np.array(logic._parameterNode.RGYNpArrOrder))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "556dfa20-80ae-44fe-8a6f-9d2cf9ed6368",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(logic._parameterNode.GetParameter(\"sammViewOptions\")==\"RED\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e8764dcb-056f-4cc5-a206-934e0b51a7b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(129.72245434031765, -121.57139587402344, -10.214302062988281, 1.0)\n",
      "(2.4310518326593353, 7.548171374262596, 4.936334692913553, 1.0)\n",
      "[2, 1, 0]\n",
      "[4.93633469 7.54817137 2.43105183]\n"
     ]
    }
   ],
   "source": [
    "s = slicer.app.layoutManager().sliceWidget('Red').mrmlSliceNode()\n",
    "mat = s.GetXYToRAS()\n",
    "temp = mat.MultiplyPoint([0,0,0,1])\n",
    "print(temp)\n",
    "ras2ijk = vtk.vtkMatrix4x4()\n",
    "logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetRASToIJKMatrix(ras2ijk)\n",
    "temp = ras2ijk.MultiplyPoint(temp)\n",
    "print(temp)\n",
    "print(logic._parameterNode.RGYNpArrOrder)\n",
    "temp = np.array([temp[0],temp[1],temp[2]])[logic._parameterNode.RGYNpArrOrder]\n",
    "print(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "56a420d0-343b-43a1-80f0-295a340c3f4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 10, 10)\n",
      "(10, 10, 10)\n"
     ]
    }
   ],
   "source": [
    "volumeNodeDataPointer = slicer.util.arrayFromVolume(\n",
    "    logic._parameterNode.GetNodeReference(\"sammInputVolume\"))\n",
    "volumeDisplayNode = logic._parameterNode.GetNodeReference(\"sammInputVolume\").GetDisplayNode()\n",
    "window = volumeDisplayNode.GetWindow()\n",
    "level = volumeDisplayNode.GetLevel()\n",
    "\n",
    "imageMin = level - window / 2\n",
    "imageMax = level + window / 2\n",
    "\n",
    "imageNormalized = (((copy.deepcopy(volumeNodeDataPointer) - imageMin).astype(\"float32\") / (imageMax - imageMin)) * 256).astype(numpy.uint8)\n",
    "print(imageNormalized.shape)\n",
    "imageNormalized = imageNormalized.transpose(2-np.array(logic._parameterNode.RGYNpArrOrder))\n",
    "print(imageNormalized.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6a94d1c9-f451-4d87-946e-747a7f02053a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "127.0\n"
     ]
    }
   ],
   "source": [
    "curslc = logic.utilGetCurrentSliceIndex()\n",
    "print(curslc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8d8f41e7-d3aa-4dfd-b192-db8c337f42f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[89, 75], [123, 102], [152, 66]]\n",
      "[[132, 35], [192, 55], [180, 95]]\n",
      "[SAMM INFO] Sent Size Command.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|##########| 256/256 [00:00<00:00, 868.94it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SAMM INFO] Sent Image.\n",
      "[SAMM INFO] Sent Embedding Computing Command.\n"
     ]
    }
   ],
   "source": [
    "logic.processInitPromptSync()\n",
    "prompt_add_point, prompt_remove_point = [], []\n",
    "curslc = logic.utilGetCurrentSliceIndex()\n",
    "numControlPoints = logic._prompt_add.GetNumberOfControlPoints()\n",
    "for i in range(numControlPoints):\n",
    "    ras = vtk.vtkVector3d(0,0,0)\n",
    "    logic._prompt_add.GetNthControlPointPosition(i,ras)\n",
    "    temp = logic._volumeRasToIjk.MultiplyPoint([ras[0],ras[1],ras[2],1])\n",
    "    temp = np.array([temp[0], temp[1], temp[2]])[logic._parameterNode.RGYNpArrOrder]\n",
    "    prompt_add_point.append(logic.utilGetPositionOnSlicer(temp))\n",
    "\n",
    "numControlPoints = logic._prompt_remove.GetNumberOfControlPoints()\n",
    "for i in range(numControlPoints):\n",
    "    ras = vtk.vtkVector3d(0,0,0)\n",
    "    logic._prompt_remove.GetNthControlPointPosition(i,ras)\n",
    "    temp = logic._volumeRasToIjk.MultiplyPoint([ras[0],ras[1],ras[2],1])\n",
    "    temp = np.array([temp[0], temp[1], temp[2]])[logic._parameterNode.RGYNpArrOrder]\n",
    "    prompt_remove_point.append(logic.utilGetPositionOnSlicer(temp))\n",
    "\n",
    "print(prompt_add_point)\n",
    "print(prompt_remove_point)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9208fd04-8733-4b6d-ba33-3bc7629bd777",
   "metadata": {},
   "outputs": [],
   "source": [
    "logic.processInitPromptSync()\n",
    "logic.processStartPromptSync()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9b10bf35-1e36-41ac-b639-91066e1c1ba4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'R'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SAMM INFO] Sent Size Command.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|##########| 10/10 [00:00<00:00, 914.07it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SAMM INFO] Sent Image.\n",
      "[SAMM INFO] Sent Embedding Computing Command.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yl/Slicer/bin/Python/slicer/util.py:2521: UserWarning: does not have observer\n",
      "  warn('does not have observer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SAMM INFO] Sent Size Command.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|##########| 10/10 [00:00<00:00, 895.15it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SAMM INFO] Sent Image.\n",
      "[SAMM INFO] Sent Embedding Computing Command.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/home/yl/software/samm/samm/SammBase/SammBaseLib/WidgetSammBase.py\", line 162, in onPushStartMaskSync\n",
      "    self.logic.processStartPromptSync()\n",
      "  File \"/home/yl/software/samm/samm/SammBase/SammBaseLib/LogicSamm.py\", line 293, in processStartPromptSync\n",
      "    self._connections.pushRequest(SammMsgType.INFERENCE, {\n",
      "  File \"/home/yl/software/samm/samm/SammBase/SammBaseLib/UtilConnections.py\", line 40, in pushRequest\n",
      "    feedback = sock.recv()\n",
      "  File \"zmq/backend/cython/socket.pyx\", line 805, in zmq.backend.cython.socket.Socket.recv\n",
      "  File \"zmq/backend/cython/socket.pyx\", line 841, in zmq.backend.cython.socket.Socket.recv\n",
      "  File \"zmq/backend/cython/socket.pyx\", line 199, in zmq.backend.cython.socket._recv_copy\n",
      "  File \"zmq/backend/cython/socket.pyx\", line 194, in zmq.backend.cython.socket._recv_copy\n",
      "  File \"zmq/backend/cython/checkrc.pxd\", line 22, in zmq.backend.cython.checkrc._check_rc\n",
      "zmq.error.Again: Resource temporarily unavailable\n",
      "\n",
      "If you suspect this is an IPython 8.14.0 bug, please report it at:\n",
      "    https://github.com/ipython/ipython/issues\n",
      "or send an email to the mailing list at ipython-dev@python.org\n",
      "\n",
      "You can print a more detailed traceback right now with \"%tb\", or use \"%debug\"\n",
      "to interactively debug it.\n",
      "\n",
      "Extra-detailed tracebacks for bug-reporting purposes can be enabled via:\n",
      "    %config Application.verbose_crash=True\n",
      "\n"
     ]
    }
   ],
   "source": [
    "logic._parameterNode.GetParameter(\"sammViewOptions\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "36be2ce9-b567-4c0e-93c5-f375de2abd86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.2 0.1 0.3]\n",
      "[0.1 0.2 0.3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.array([0.1, 0.2, 0.3])\n",
    "i = np.array([1,0,2])\n",
    "y = x[i]\n",
    "print(y)\n",
    "ii = i.argsort()\n",
    "z = y[ii]\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "237453be-e899-4000-ae46-4446106d99e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.]\n",
      "  [0.]\n",
      "  [0.]]\n",
      "\n",
      " [[0.]\n",
      "  [0.]\n",
      "  [0.]]]\n"
     ]
    }
   ],
   "source": [
    "logic._parameterNode.RGYNpArrOrder\n",
    "x = np.array([0.1, 0.2, 0.3])\n",
    "y = x[2-np.array(logic._parameterNode.RGYNpArrOrder)]\n",
    "ii = np.array(2-np.array(logic._parameterNode.RGYNpArrOrder)).argsort()\n",
    "z = y[ii]\n",
    "z = np.zeros((1,2,3))\n",
    "print(z.reshape((2,3,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "92be4fc6-f838-47d0-ae4e-47c2598eb353",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.array([1,2,3,4]).reshape([1,4])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4e83cab3-759b-4bf4-a2ff-2f0134331451",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print([]==None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "512d3635-580d-443a-b011-f51e4bc4f952",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2],[3,4]])\n",
    "print(np.amax(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9618dde5-c7ad-4fef-8dd8-f6d120ad7bb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Slicer 5.2",
   "language": "python",
   "name": "slicer-5.2"
  },
  "language_info": {
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
